#!/bin/bash

if [[ "$1" == "--help" ]]; then
echo "usage: crover [days between versions] [date]"
echo ""
echo "crover generates a version number for Arch Linux based"
echo "on time elapsed since the operating system was initially"
echo "released (March 11, 2002)"
echo ""
echo "the version number takes the form of X.Y, in which X"
echo "denotes the age of Arch Linux in years and Y denotes"
echo "elapsed subdivisions of the current year"
echo ""
echo "by default, crover uses the current date and subdivides"
echo "the year into 7-day periods; add arguments to customize"
echo "these parameters"
echo ""
echo "- example -"
echo "desired output: version for March 21, 2002 with 1-day subdivision"
echo "command: crover 1 20020321"
echo "output: Arch Linux 0.10"
exit
fi

subdivision=$1
date=$2

[ -z $1 ] && subdivision=7
[ -z $2 ] && date=`date +%Y%m%d`

thissecond=$(date +%s -d $date)
thisyear=$(echo $date | head -c4)
lastyear=$(($thisyear-1))
birthday=0311
birthdaythisyear=$(date +%s -d $thisyear$birthday)
birthdaylastyear=$(date +%s -d $lastyear$birthday)

if [[ "$thissecond" -lt "$birthdaythisyear" ]]; then
 versionyear=$(($lastyear-2002))
 versionsub=$((($thissecond-birthdaylastyear)/(86400*$subdivision)))
else
 versionyear=$(($thisyear-2002))
 versionsub=$((($thissecond-birthdaythisyear)/(86400*$subdivision)))
fi

echo "Arch Linux $versionyear.$versionsub"
